package com.cupdata.marathon.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cupdata.marathon.common.KafkaSender;
import com.cupdata.marathon.common.RedisService;
import com.cupdata.marathon.definition.CouponTp;
import com.cupdata.marathon.definition.DownRslt;
import com.cupdata.marathon.definition.OperQueryRslt;
import com.cupdata.marathon.definition.RspCd;
import com.cupdata.marathon.entity.TblHackCouponDownTransLog;
import com.cupdata.marathon.mapper.TblHackCouponDownTransLogMapper;
import com.cupdata.marathon.param.acctquery.AcctQueryResponse;
import com.cupdata.marathon.param.download.DownloadRequest;
import com.cupdata.marathon.param.download.DownloadResponse;
import com.cupdata.marathon.service.ITblHackCouponDownTransLogService;
import com.cupdata.marathon.utils.GsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Service
public class TblHackCouponDownTransLogServiceImpl extends ServiceImpl<TblHackCouponDownTransLogMapper, TblHackCouponDownTransLog> implements ITblHackCouponDownTransLogService {

    @Autowired
    KafkaSender kafkaSender;

    @Autowired
    RedisService redisService;

    @Value(value = "${topicName.topicTrans}")
    private String topicTrans;

    /**
     * 创建流水记录
     */

    @Override
    public void save(DownloadRequest request, DownloadResponse response, LocalDateTime transCfmTs) {
        TblHackCouponDownTransLog entity = new TblHackCouponDownTransLog();
        // 请求报文中 userId
        entity.setUserId(request.getUserId());
        // 请求报文中 transId
        entity.setTransId(request.getTransId());
        // 请求报文中 couponTp
        entity.setCouponTp(request.getCouponTp());
        // 请求报文中 cityCd
        entity.setCityCd(request.getCityCd());

        // 请求报文中的 transTime
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime transTime = LocalDateTime.parse(request.getTransTime(), dtf);
        entity.setTransCrtTs(transTime);

        // 先到先得考核使用: 获取消费券成功/失败的同时生成一个确认时间
        entity.setTransCfmTs(transCfmTs);

        LocalDateTime now = LocalDateTime.now();
        entity.setRecCrtTs(now);
        entity.setRecUpdTs(now);

        // 会员服务返回的属性，若请求非法或调用会员服务异常填0
        entity.setMemTp(response.getMemTp());
        // 应答报文中 couponId
        entity.setCouponId(response.getCouponId());
        // 应答报文中 rspCd
        String rspCd = response.getRspCd();
        entity.setRspCd(rspCd);
        // 应答报文中 rspMsg (选填)
//        entity.setRspMsg(response.getRspMsg());

        if (RspCd.SUCCESS.getCode().equals(rspCd)) {
            // 应答码 00 则必填
            String downRslt = response.getDownRslt();
            entity.setDownRslt(downRslt);

            if (DownRslt.R000000.getCode().equals(downRslt)) {
                // 领到则必填
                entity.setDownAmt(CouponTp.getValue(entity.getCouponTp()));
            }
        }


        kafkaSender.sendMessage(topicTrans, GsonUtils.gson.toJson(entity));
//        this.save(entity);

    }

    /**
     * 更新流水记录
     */
    @Override
    public void update(String couponId, AcctQueryResponse response) {
        TblHackCouponDownTransLog entity = new TblHackCouponDownTransLog();
        String operRslt = response.getOperRslt();
        // 动账查询接口返回"入账成功"
        if (OperQueryRslt.SUCCESS.getCode().equals(operRslt)) {
            // 更新领取结果
            entity.setDownRslt(DownRslt.R000000.getCode());
            // 设置记录更新时间
            entity.setRecUpdTs(LocalDateTime.now());

            updateRec(couponId, entity);
        }

        // 动账查询接口返回"入账失败"
        if (OperQueryRslt.FAIL.getCode().equals(operRslt)) {
            // 更新领取结果
            entity.setDownRslt(DownRslt.R300010.getCode());
            // 清空领取金额
            entity.setDownAmt(0);
            // 设置记录更新时间
            entity.setRecUpdTs(LocalDateTime.now());

            updateRec(couponId, entity);
        }
    }

    private void updateRec(String couponId, TblHackCouponDownTransLog entity) {
        UpdateWrapper<TblHackCouponDownTransLog> wrapper = new UpdateWrapper<>();
        wrapper.eq("COUPON_ID", couponId);
        this.update(entity, wrapper);
    }
}
